<template>
  <div id="home" class="wrapper">
    <NavBar class="home-nav">
      <div slot="center">购物街</div>
    </NavBar>
    <TabControl
      ref="tab1"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      v-show="isTabFixed"
      :class="{ tabFixedStyle: isTabFixed }"
    />
    <Scroll
      ref="scroll"
      class="content"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
    
      <Carsouel :list="banners"> </Carsouel>
      <RecommendView :recommends="recommends" />
      <FeatureView />
      <TabControl
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tab2"
      />
      <goods-list :goods="showGoods" />
    </Scroll>
    <BackTop @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
// import BScroll from "better-scroll";自定义事件native

import debounce from "lodash/debounce";
import RecommendView from "./Recommand";
import FeatureView from "./FeatureView";

import { getHomeMultidata, getHomeGoods } from "@/api/home";

import BackTop from "@/components/backTop";
// import { debounce } from "@/common/utils";
export default {
  name: "HomelIndex",

  data() {
    return {
      banners: [],
      recommends: [],

      goods: {
        pop: { page: 0, List: [] },
        new: { page: 0, List: [] },
        sell: { page: 0, List: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  components: {
    RecommendView,
    FeatureView,

    BackTop,
  },
  created() {
    // 1.请求多个数据
    this.getHomeMultidata();

    //  2.请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
    // 1.图片加载完成的事件监听
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
    console.log(this.saveY);
    this.$refs.scroll.refresh()
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY();
    console.log(this.saveY);
  },
  mounted() {
    //
    const refresh = debounce(this.$refs.scroll.refresh, 50);
    this.$bus.$on("itemImageLoad", () => {
      refresh();
    }); //
    // });
    // getHomeGoods()
    // this.goods[type].List=res.data.list
    // this.goods[type].page += 1;
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].List;
    },
  },
  methods: {
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }

      this.$refs.tab1.currentIndex = index;
      this.$refs.tab2.currentIndex = index;
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    swiperImageLoad() {},

    // getGoods(type) {
    //   mockRequests.get(`/banner`).then((res) => {
    //     this.goods[type].List = res.data[type];
    //     this.$refs.scroll.finishPullUp();
    //   });
    // },
    // 3.监听scroll滚动到底部

    loadMore() {
      this.getHomeGoods(this.currentType);
    },
    //网络请求方法
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
 
        this.goods[type].List.push(...res.data.list);
        this.goods[type].page += 1;

        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp();
      });
    },
    // 2.监听滚动的位置
    contentScroll(position) {
      // console.log(position);
      // 1.判断BackTop是否显示
      this.isShowBackTop = -position.y > 1000;

      // 2.决定tabControl是否吸顶(position: fixed)
      this.tabOffsetTop = this.$refs.tab2.$el.offsetTop;
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
  },
};
</script>

<style scoped>
#home {
  /*padding-top: 44px;//100%视口*/
  height: 100vh;
  position: relative;
}

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
